/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ecom.shell;

import ecom.beans.entity.Accounts;
import ecom.beans.session.EcomAdminRemote;
import java.io.PrintStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import shell.ShellCommand;
import shell.ShellContext;

/**
 *
 * @author Amina
 */
public class AccountCommandImpl implements ShellCommand{
    private ShellContext _context;
    public AccountCommandImpl(ShellContext context)
	{
		_context = context;
	}
    public void execute(String cmdline, PrintStream out, PrintStream err)
    {
        EcomAdminRemote admin = null;
        try {
            if (_context.getVar("ecomAdmin")==null){
                Context ctx = new InitialContext();
                admin = (EcomAdminRemote) ctx.lookup(EcomAdminRemote.class.getName());
                _context.setVar("ecomAdmin", admin);
            }
            else{
                admin = (EcomAdminRemote) _context.getVar("ecomAdmin");
            }
            
            out.append("Login\tIBAN\tOwner\tBalance\n");

            List<Accounts> accounts = admin.getAllAccount();
            
            for(Accounts account : accounts)
            {
                out.append(account.getClients().getEmail() + "\t" + account.getIban() + "\t" + account.getOwner() + "\t" + Double.toString(account.getBalance()) + "\n");
                //out.println(account.getBalance());
            }
        } catch (NamingException ex) {
            Logger.getLogger(AccountCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getName() {
        return "account";
    }

    public String getShortDescription() {
        return "List all accounts";
    }

   public String getUsage() {
        return "account";
    }



}
